package de.msiggi.Sportsdata.Tools;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import de.msiggi.Sportsdata.Testfiles.MatchDateComparator;
import de.msiggi.Sportsdata.Webservices.Matchdata;

public class GenerateGameDays {
	
	TreeMap<Integer, GameDay> gameDays = new TreeMap<Integer, GameDay>();
	
	public List<GameDay> generateGameDays(Matchdata [] matchesOfOneSeason){
		TreeMap<Integer, GameDay> gameDays = new TreeMap<Integer, GameDay>();
		for (Matchdata matchdata : matchesOfOneSeason) {
			int id = matchdata.getGroupID();
			if(gameDays.containsKey(id)){
				GameDay tmp = gameDays.get(id);
				tmp.addMatch(matchdata);
			} else {
				GameDay newGaDa = new GameDay(matchdata.getGroupName());
				newGaDa.addMatch(matchdata);
				gameDays.put(id, newGaDa);
			}
		}
		
		Set<Integer> keys = gameDays.keySet();
		List<GameDay> gds = new ArrayList<GameDay>(gameDays.size());
		for (Integer integer : keys) {
			gds.add(gameDays.get(integer));
		}
		
		for (GameDay gameDay : gds) {
			Collections.sort(gameDay.getMatches(), new MatchDateComparator());
		}
		return gds;
		
	}
	
	public TreeMap<Integer, GameDay> generateSeasonWithGameDaySplit(Matchdata [] matchesOfOneSeason){
		for (Matchdata matchdata : matchesOfOneSeason) {
			int id = matchdata.getGroupID();
			if(gameDays.containsKey(id)){
				GameDay tmp = gameDays.get(id);
				tmp.addMatch(matchdata);
			} else {
				GameDay newGaDa = new GameDay(matchdata.getGroupName());
				newGaDa.addMatch(matchdata);
				gameDays.put(id, newGaDa);
			}
		}
		return gameDays;
		
	}

}
